package com.mygame.center.controller;

import com.mygame.center.GameEngine;
import com.mygame.common.component.RequestPacket;
import com.mygame.common.component.RouteInfo;
import com.mygame.common.eventsystem.GameEventListener;
import com.mygame.common.eventsystem.GameEventSystem;
import io.nats.streaming.StreamingConnection;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

@RestController
@RequestMapping("test")
public class TestController {

    @Resource(name = "streamingConnection")
    private StreamingConnection connection;

    @RequestMapping("https")
    public Object getHttps() {
        return "Hello ,Https";
    }

    @GetMapping("/send")
    public String asd(@RequestParam String str) throws InterruptedException, TimeoutException, IOException {
        System.out.println("发送消息: " + str);
        connection.publish("bbb", str.getBytes(StandardCharsets.UTF_8));
        return "asd";
    }

    @GetMapping("/test")
    public String test(@RequestParam String str) throws InterruptedException, TimeoutException, IOException {
        long start = System.currentTimeMillis();
        RouteInfo routeInfo = GameEngine.me().route().getRouteInfo("GATE_SERVER.CommonHandler.game.test");
//        Object[] parameters = buildParameters(routeInfo, packet);
        for (int i = 1; i < 10; i++) {
            if (routeInfo.method.isVoid()) {
                RequestPacket requestPacket = new RequestPacket();
                routeInfo.invoke(requestPacket);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start));
        return "ok";
    }


    @GetMapping("/sendEvent")
    public String assendEventd(@RequestParam String str) throws InterruptedException, TimeoutException, IOException {
        PlayerUpgradeLevelEvent event = new PlayerUpgradeLevelEvent();
        event.setPlayerId(1);
        GameEventSystem.sendGameEvent(this, event);
        return "asd";
    }

    @GameEventListener(PlayerUpgradeLevelEvent.class)
    public void playerUpgradeEvent(Object origin, PlayerUpgradeLevelEvent event) {
        System.out.println("任务接收到角色升级事件：" + event.getClass().getName());
        //在这里处理相应的业务逻辑。
    }
}
